<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Manager;

use Doctrine\ORM\EntityManager;
use Agp\CoreBundle\Entity\Pessoa;
use Agp\CoreBundle\Manager\BaseManager;
use Agp\CoreBundle\Entity\Grupo;
use Agp\AdminBundle\Security\Permission\Pessoa\MaskBuilder;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;

/**
 * Description of PessoaManager
 *
 * @author bondcs
 */
class PessoaAgapeManager extends BaseManager{
    
    public function persist($entity)
    {
        $empresaInc = $this->repository->getLastInc($entity->getEmpresa())+1;
        $entity->setEmpresaInc($empresaInc);
        parent::persist($entity);
        $this->setAclDefault($entity);
    }
    
    public function updateWithAcl($entity, $roleOld) {
        parent::update($entity);
        if ($entity->getFirstRole()->getRole() != $roleOld->getRole()){
           $this->setAclDefault($entity); 
        }
    }

    public function setAclDefault($user){
        $aclManager = $this->container->get('problematic.acl_manager');
        $role = $user->getFirstRole();
        $permissoes = Grupo::geraAclDefault($role->getRole());
        foreach ($permissoes as $key => $grupo){
            $class = $key;
            $instancia = new $class;
            $aclManager->revokeAllClassPermissions($instancia, $user);
            $builder = new MaskBuilder();
            foreach ($grupo as $mask){
                if (true === $this->container->get('security.context')->isGranted($mask, new ObjectIdentity('class', $class))){
                    $builder->add($mask);
                }
            }
            $mask = $builder->get();
            $aclManager->setClassPermission($instancia, $mask, $user);
        }
    
    }
    
}

?>
